package com.cmcc.aregister.ui.adapter;

import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.constant.ConfigUtilHelper;
import com.cmcc.aregister.cs.model.MemberModel;

public class ChoiceMemberAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    public List<MemberModel> list;
    private Handler handler;
    private Context context;

    public ChoiceMemberAdapter(Context context, List<MemberModel> list,Handler handler) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.list = list;
        this.handler = handler;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MemberHolder holder = null;
        if (convertView == null) {
            holder = new MemberHolder();
            convertView = inflater.inflate(R.layout.a_choice_member, null);
            holder.idView = (TextView) convertView.findViewById(R.id.member_id);
            holder.nameView = (TextView) convertView.findViewById(R.id.member_name);
            holder.phoneView = (TextView) convertView.findViewById(R.id.member_phone);
            holder.relationView = (TextView) convertView.findViewById(R.id.member_relation);
            holder.relationTxtView = (TextView) convertView.findViewById(R.id.member_relation_text);
            holder.detailButton = (Button) convertView.findViewById(R.id.detail);
            convertView.setTag(holder);
        }
        else {
            holder = (MemberHolder) convertView.getTag();
        }
        MemberModel member = list.get(position);
        holder.detailButton.setTag(position);
        holder.idView.setText(member.member_id);
        holder.nameView.setText(member.truename);
        holder.phoneView.setText(member.phone);
        holder.relationView.setText(String.valueOf(member.relation));
        holder.relationTxtView.setText(ConfigUtilHelper.getRelationText(context,member.relation,member.sex));
        holder.detailButton.setOnClickListener(onClickListener);
        return convertView;
    }
    
    OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            handler.sendMessage(handler.obtainMessage(3, v.getTag()));
        }
    };

    static final class MemberHolder {
        TextView idView;
        TextView nameView;
        TextView phoneView;
        TextView relationView;
        TextView relationTxtView;
        Button detailButton;
    }

}
